# -*- coding: utf-8 -*-

"""
Created on Fri Jun  8 18:43:24 2018

@author: Administrator
"""


import os
#%%
#得到目录start_path之后到文件的最深level
def get_deep_level(start_path):
    i = 0
    level_need = 0
    for root, dirs, files in os.walk(start_path):
        i = i + 1
        #print(root)
        level = root.replace(start_path, '').count(os.sep)
        if level_need <= level:
            level_need = level
        print('procession of get_deep_level',i)    
        print('-------------------------------- \n')
    return level_need


#%%
#生成n级目录树（不包括文件），并存放在txt文件中
def list_tree(start_path,outp_path,level_need):
    filename = r'\tree_of_' + os.path.split(start_path)[1] + r'_level_' + str(level_need) + r'.txt'
    fileSave = open(outp_path + filename,'w')
    i = 0
    for root, dirs, files in os.walk(start_path):
        i = i + 1
        #print(root)
        level = root.replace(start_path, '').count(os.sep)
        #print(root)
        #print(level)
        dir_indent = "|   " * (level-1) + "|-- "
        #print(dir_indent)
        file_indent = "|   " * level + "|-- "
        #print(file_indent)
        if not level:
            fileSave.write('..' + '\n')
        else:
            if level <= level_need:
                fileSave.write('{}{}'.format(dir_indent, os.path.basename(root)) + r'    ' + str(len(dirs)) +  r'    ' + str(len(files)) + '\n')
        print('procession of list_tree','level_need=',level_need,i)
        print('...................................\n')
    fileSave.close()
	
	
#%%
#生成完整的目录树，并存放在txt文件中
def list_files(start_path,outp_path):
    filename = r'\file_list_of_' + os.path.split(start_path)[1]  + r'.txt'
    fileSave = open(outp_path + filename,'w')
    i = 0
    for root, dirs, files in os.walk(start_path):
        i = i + 1
        #print(root)
        level = root.replace(start_path, '').count(os.sep)
        #print(root)
        #print(level)
        dir_indent = "|   " * (level-1) + "|-- "
        #print(dir_indent)
        file_indent = "|   " * level + "|-- "
        #print(file_indent)
        if not level:
            fileSave.write('..' + '\n')
        else:
            fileSave.write('{}{}'.format(dir_indent, os.path.basename(root)) + r'   No._of_files:  '+ str(len(files)) + '\n')
        j = 0
        file_size_largest = ['placeholder',0]
        file_size_smallest = ['placeholder',0]
        if len(files) > 0:
            for f in files:
                file_path = root + '\\' + f
                file_size = os.path.getsize(file_path)
                if file_size_largest[1] <= file_size:
                    file_size_largest = [f,file_size] 
            file_size_largest[1] = str( round(file_size_largest[1]/(1024*1024),3) )
            fileSave.write('{}{}'.format(file_indent, file_size_largest[0]) + '   largest_size:  ' +  file_size_largest[1] + 'MB' + '\n')
        print('procession of list_files',i)
        print(',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n')
    fileSave.close()
	
	
#level_need = get_deep_level('F:\走势图')
#level_need
#level_need = get_deep_level('H:\原始数据\BOLD')
#level_need

#list_tree('F:\走势图','F:',1)
#list_tree('H:\原始数据\BOLD','H:',1)


#调用list_tree、list_files两个函数，生成各种层级的文件目录，便于对数据的查看。
#第一个参数是需要peek的位置，第二个参数是存放生成的txt文件书的位置。
def peek_path_struct(start_path,outp_path):
    level_need = get_deep_level(start_path)
    print(level_need)
    for i in range(1,level_need+1):
        print(i)
        list_tree(start_path,outp_path,i)
    list_files(start_path,outp_path)
	
#peek_path_struct('H:\data\huxianzhang','H:\data')
#peek_path_struct(r'H:\regular',r'H:\structure')